package com.dream.xcarrecorder.rec;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.util.ArraySet;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.View;

import com.dream.libxrec.camera.ICamera;
import com.dream.libxrec.camera.XCamera2;
import com.dream.libxrec.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class RecHelper {

    private final ArrayList<CameraHelper> mChList = new ArrayList<>();
    private final AudioHelper mAudioHelper = AudioHelper.getInstance();
    private Context mContext = null;

    private RecHelper() {}

    public static RecHelper getInstance() {
        return RecHelper.SingletonHolder.sInstance;
    }

    private static class SingletonHolder{
        private static final RecHelper sInstance = new  RecHelper();
    }


    public void init(Context ct){
        this.mContext = ct;
    }

    public int getCameraCount(){
        return mChList.size();
    }

    public void setPreview(int channel,Surface surface){
        CameraHelper ch = mChList.get(channel);
        if (ch != null){
            ch.setPreview(surface);
        }
    }

    public void takePhoto(int channel,
                          CameraHelper.ITakePhotoListener listener){

        if (mChList.size() <= channel){
            return;
        }

        CameraHelper ch = mChList.get(channel);
        if (ch != null){
            ch.takePicture(listener);
        }
    }

    public synchronized void start() throws Exception{
        if (!mAudioHelper.isRun()){
            mAudioHelper.init(mContext);
            mAudioHelper.start();
        }

        List<ICamera> list = listCamera();
        Log.d("RecHelper","camera count:"+list.size());
        for (ICamera c:list){
            CameraHelper ch = new CameraHelper();
            ch.init(mContext,c,352,288);
            mChList.add(ch);
        }
        for (CameraHelper c:mChList){
            if (!c.isRun()){
                c.start();
            }
        }
    }

    public synchronized boolean isRun(){
        for (CameraHelper c:mChList){
            if (!c.isRun()){
                return false;
            }
        }

        return mAudioHelper.isRun();
    }

    public void stop(){
        mAudioHelper.stop();
        for (CameraHelper c:mChList){
            c.stop();
        }
        mChList.clear();
    }

    private List<ICamera> listCamera() throws Exception{
        boolean isScreenVertical =
                mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;

        CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
        String[] cameraIds = manager.getCameraIdList();

        List<ICamera> list = new ArrayList<>();
        for (String id:cameraIds){
            CameraCharacteristics mCameraCharacteristics =
                    manager.getCameraCharacteristics(id);

            int rotation = isScreenVertical?90:0;//竖屏录像需要旋转90°

            boolean isMirror =
                    CameraCharacteristics.LENS_FACING_FRONT == mCameraCharacteristics.get(CameraCharacteristics.LENS_FACING);//前摄需要左右镜像

            StreamConfigurationMap map = mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            Size[] sizes =
                    map.getOutputSizes(SurfaceTexture.class);

            //从最高分辨率往后取，最大为1920*1080P,录像不需要过高的分辨率
            int index = 0;
            for (int i = 0;i<sizes.length;i++){
                Size s = sizes[i];
                if (Math.max(s.getHeight(),s.getWidth()) <= 1920){
                    index = i;
                    break;
                }
            }
            Size size = sizes[index];

            ICamera camera = new XCamera2();
            camera.init(mContext,id,size,rotation,isMirror);

            list.add(camera);
        }

        return list;
    }
}
